<template>
	<div class="back_contain_n Organization">
		<div class="Search_Top_Input">
			<div class="addNew">
				<span @click="addOrganization()" v-if="hasRole('org:system:add')"><i class="el-icon-circle-plus-outline"></i> 新建</span>
				<span class="edit" @click="editOrganization()" v-if="hasRole('org:system:edit')"><i class="el-icon-edit-outline"></i> 编辑</span>
				<span class="goBack" @click="handleDelete" v-if="hasRole('org:system:del')"><i class="el-icon-delete"></i> 删除</span>
			</div>
		</div>
		<div class="tree mytree">
			<div class="img_Organization">
				<img class="tree_icon" :src=treeIcon>
				<div class="el-dev-divider"></div>
			</div>
			<el-tree :data="deptList"
					 :props="defaultProps"
					 class="tree_Organization"
					 show-checkbox
					 node-key="id"
					 ref="tree"
					 :default-expanded-keys="expandedKeys"
					 highlight-current
					 check-strictly
			></el-tree>
		</div>
		<!-- 新建弹框 -->
		<div class="zll-dialog">
			<popout :title="type=='add'?'组织架构 · 新建':'组织架构 · 编辑'" :visible.sync="addDialog" v-show="addDialog" class="minSize">
				<Add ref="add" slot="content" :type="type" :editData="editData" :deptList="deptList"
					 @addForm="getFormData"></Add>
				<template slot="bottom">
					<p class="zll-botton" @click="saveDepts">提 交</p>
				</template>
			</popout>
		</div>
	</div>
</template>

<script>
	import Add from './Add'
	import {getStore} from "@/plugins/localStore";

	export default {
		data() {
			return {
				type: '',
				addDialog: false, //角色弹框
				defaultProps: {
					children: 'children',
					label: 'deptName'
				},
				deptList: [],
				editData: {},
				expandedKeys: [],

				treeIcon: require('@/assets/back_manage_img/logo.png'),
			}
		},
		methods: {
			getSysHeadImg() {
				let self = this;
				self.$http({
					url: "/jinl/file/queryFileList",
					method: "post",
					params: {type: '3', check: '1',notToken:true}
				}).then(resp => {
					if (resp.success) {
						if (resp.result.length > 0) {
							self.treeIcon = resp.result[0].fileUrl;
						}

					}
				});
			},
			saveDepts() {
				if (this.type == 'add') {
					this.$refs.add.saveDept('addForm')
				}
				if (this.type == 'edit') {
					this.$refs.add.saveEditDept('addForm')
				}
			},

			addOrganization() {
				this.addDialog = true;
				this.editData = {id: ""};
				this.type = "add"
			},
			editOrganization() {
				let checkNodes = this.$refs.tree.getCheckedNodes();
				if (!checkNodes || checkNodes.length != 1) {
					this.$notify({
						title: '提示',
						message: "选择一个节点！",
						type: 'warning'
					});
					return
				}
				this.addDialog = true;
				console.log(checkNodes[0])
				this.editData = Object.assign({}, checkNodes[0]);
				this.type = "edit"
			},
			getFormData(data) {
				this.addDialog = false
			},

			getList() {
				let self = this;
				self.expandedKeys = [];
				self.$http({
					url: "/jinl/dept/getDepartmentListTree",
					method: "post",
				}).then(resp => {
					if (resp.success) {
						self.deptList = [];
						if (resp.result.length > 0) {
							self.deptList = resp.result;
							self.expandedKeys.push(self.deptList[0].id)
						}
					}
				});
			},
			handleDelete() {
				let self = this;
				let user = getStore();
				let checkNodes = self.$refs.tree.getCheckedNodes();
				if (!checkNodes || checkNodes.length < 1) {
					self.$notify({
						title: '提示',
						message: "至少选择一个节点！",
						type: 'warning'
					});
					return
				}
				let ids = [];
				for (let node of checkNodes) {
					ids.push(node.id);
				}
				self.$confirm('确定删除该信息？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					self.$http({
						url: "/jinl/dept/deleteDeptDetail",
						method: "post",
						data: JSON.stringify({ids: ids, userName: user.userName, userId: user.id}),
						dataType: 'json',
						contentType: "application/json",
					}).then(resp => {
						if (resp.success) {
							self.getList();
							self.$notify({
								title: '提示',
								message: "删除成功！",
								type: 'success'
							});
							return
						}
						self.$notify({
							title: '提示',
							message: "操作失败，请联系管理员！",
							type: 'error'
						});
					});
				});
			},
		},
		mounted() {
			let self = this;
			self.getList();
			self.getSysHeadImg();
			self.$eventBus.$on("getDeptList", function () {
				self.getList();
				self.addDialog = false;
			})
		},
		components: {
			Add,
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/style/SearchTop.scss";

	.Organization {
		.mytree {
			margin-left: 230px;
			margin-top: 80px;
			position: relative;

			.img_Organization {
				position: absolute;
				height: 50px;
				border-bottom: 1px solid #bebfbe;
				left: -166px;
				top: -62px;
				display: flex;
				align-items: center;
				justify-content: center;

				.tree_icon {
					width: 150px;
					max-height: 45px;
				}

				.el-dev-divider {
					background-color: #bebfbe;
					position: absolute;
					right: -18px;
					bottom: -7px;
					height: 1px;
					width: 20px;
					-webkit-transform: rotate(40deg);
					transform: rotate(40deg);
				}
			}
		}
	}
</style>
